<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="问题" prop="qaTitle">
        <el-input v-model="queryParams.qaTitle" type="text" placeholder="请输入" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="qaList">

      <el-table-column label="ID" align="center" prop="qaId" />
      <el-table-column prop="qaTitle" label="问题" width="400" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :model="form" ref="form" :rules="rules" label-width="80px">
        <el-form-item label="问题" prop="qaTitle">
          <el-input v-model="form.qaTitle" placeholder="请输入问题"></el-input>
        </el-form-item>
        <el-form-item label="答案" prop="qaContent">
          <Editor v-model="form.qaContent" :minHeight="300"></Editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const contentValidate = (rule, value, callback) => {

  if (!value || value == '<p><br></p>') {
    callback(new Error('答案不能为空'))
  }
  else {
    callback()
  }
}

import { listHomeQa, getHomeQa, addHomeQa, updateHomeQa, deleteHomeQa } from '@/api/spoken/qa.js'
export default {
  data() {
    return {
      form: {},
      // 查询参数
      queryParams: {
        qaTitle: undefined,
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      qaList: [],
      rules: {
        qaTitle: [
          { required: true, message: "问题不能为空", trigger: "blur" }
        ],
        qaContent: [
          { required: true, validator: contentValidate, trigger: "blur" }
        ]

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.title = '新增'
      this.open = true

    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listHomeQa(this.queryParams).then(response => {
        this.qaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        qaContent: undefined,
        qaTitle: undefined,
        qaId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getHomeQa(row.qaId).then(res => {
        this.form = res.data
        this.open = true;
        this.title = "修改";
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 编辑
          if (this.form.qaId) {
            updateHomeQa(this.form).then(res => {
              this.$modal.msgSuccess("修改成功");
              this.open = false
              this.getList();
            })
          } else {  // 新建
            addHomeQa(this.form).then(res => {
              this.$modal.msgSuccess("新增成功");
              this.open = false
              this.getList();
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除？').then(function () {
        return deleteHomeQa(row.qaId);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(() => { });
    }
  }
}
</script>
<style lang="scss" scoped></style>